#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QModelIndex>
#include <QtCore/QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QAction>
#include <QSystemTrayIcon>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QRect rect;

    rect = MainWindow::geometry();

    QPoint point=rect.topLeft();

    ui->tabWidget->resize(point.x()+rect.width(), point.y()+rect.height());

    createActions();
    createTrayIcon();

    connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

    trayIcon->setIcon(QIcon(":/images/trash"));

    trayIcon->show();

    setWindowTitle(tr("Serial Port Test"));
    setWindowIcon(QIcon(":/images/heart"));

    // Example use QSerialPortInfo
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug() << "Name        : " << info.portName();
        qDebug() << "Description : " << info.description();
        qDebug() << "Manufacturer: " << info.manufacturer();


        ui->textBrowser->append(info.portName());
        ui->textBrowser->append(info.description());
        ui->textBrowser->append(info.manufacturer());
        ui->comboBox->addItem(info.portName());
        // Example use QSerialPort


    }
    serial.setPortName(ui->comboBox->currentText());
    serial.setDataBits(QSerialPort::Data8);
    serial.setBaudRate(QSerialPort::Baud115200);
    serial.setParity(QSerialPort::NoParity);

    //init listwitget
    ui->listWidget->insertItem(0, "select");

}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        //iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
        break;
    case QSystemTrayIcon::MiddleClick:
        showMessage();
        break;
    default:
        ;
    }
}

void MainWindow::showMessage()
{
//    showIconCheckBox->setChecked(true);
//    QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
//            typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
//    trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
//                          durationSpinBox->value() * 1000);
    trayIcon->showMessage("trayIcon Message", "message hello", QSystemTrayIcon::Information, 1000);
}

void MainWindow::on_pushButton_clicked()
{

}

void MainWindow::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked())
    {
        ui->radioButton->toggle();
    } else {
        ui->radioButton->toggle();
    }
}

void MainWindow::on_commandLinkButton_clicked()
{

    ui->radioButton->toggle();
    ui->textBrowser->append("123");

}

void MainWindow::on_pushButton_pressed()
{

}


void MainWindow::on_comboBox_activated(const QString &arg1)
{
    // ui->textBrowser->append(ui->comboBox->currentText());
    serial.close();
    QByteArray arry;
    arry.append("/dev/tty.");
    arry +=arg1;
    ui->textBrowser->append(arry);
    serial.setPortName(arry);
    serial.setDataBits(QSerialPort::Data8);
    serial.setBaudRate(QSerialPort::Baud115200);
    serial.setParity(QSerialPort::NoParity);


}

void MainWindow::on_pushButton_released()
{
    if(!serial.isOpen()) {
        if(!serial.open(QIODevice::ReadWrite)) {
            qDebug()<<"open err!";
            return;
        }
    }
    serial.write("asdfsadf");
}

void MainWindow::on_pushButton_5_released()
{
    QString filename=QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Images (*.png *.xpm *.jpg)"));

    if(!filename.isEmpty()) {
        qDebug()<<"select file :" << filename;
        ui->listWidget->insertItem(0,filename);
    }

}

void MainWindow::on_pushButton_7_released()
{
    //    QStringList strl = ;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug() << "Name        : " << info.portName();
        qDebug() << "Description : " << info.description();
        qDebug() << "Manufacturer: " << info.manufacturer();


        ui->textBrowser->append(info.portName());
        ui->textBrowser->append(info.description());
        ui->textBrowser->append(info.manufacturer());
        ui->textBrowser->append(info.systemLocation());
        ui->comboBox->insertItem(QComboBox::InsertAtTop,info.portName());
        // Example use QSerialPort


    }

}

void MainWindow::on_actionConnect_3_triggered()
{
}

void MainWindow::on_actionQuit_triggered()
{
    //    qDebug()<<"actionQuit triggered!"<<endl;
    //    if (QMessageBox::Yes == QMessageBox::warning(this,tr("CT Control View"),
    //                                   tr("Do you really want to exit ?"),
    //                               QMessageBox::Yes, QMessageBox::No))
    {
        this->close();
    }
}

void MainWindow::on_actionReflash_triggered()
{
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug() << "Name        : " << info.portName();
        qDebug() << "Description : " << info.description();
        qDebug() << "Manufacturer: " << info.manufacturer();

        ui->textBrowser->append(info.portName());
        ui->textBrowser->append(info.description());
        ui->textBrowser->append(info.manufacturer());
        ui->textBrowser->append(info.systemLocation());
        ui->comboBox->insertItem(QComboBox::InsertAtTop,info.portName());
        // Example use QSerialPort
        // Example use QSerialPort
    }

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::warning(this,tr("CT Control View"),
                                   tr("允许后台运行 ?"),
                                   QMessageBox::Yes | QMessageBox::Default,
                                   QMessageBox::No,
                                   Qt::Sheet);
    switch (ret) {
    case QMessageBox::Yes:
        hide();
        event->ignore();
        break;
    default:
        event->accept();
        break;
    }
}

void MainWindow::createActions()
{
    minimizeAction = new QAction(tr("Mi&nimize"), this);
    minimizeAction->setIcon(QIcon(":/images/exit"));
    connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

    maximizeAction = new QAction(tr("Ma&ximize"), this);
    connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(maximizeAction);
    trayIconMenu->addAction(restoreAction);
    QMenu * menu1 = trayIconMenu->addMenu("popMenu");
    menu1->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}

void MainWindow::messageClicked()
{
    QMessageBox::information(0, tr("Systray"),
                             tr("Sorry, I already gave what help I could.\n"
                                "Maybe you should try asking a human?"));
}

void MainWindow::setIcon(int index)
{

}

void MainWindow::on_actionSettings_triggered()
{
//    Serial_setting *set = new Serial_setting(this);
//    set->setWindowTitle("Serial Setting");
//    set->setAttribute(Qt::WA_DeleteOnClose);
//    //set->show();
//    set->exec();
}
